// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/05/28 14:04:52
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yunchang;
using UnityEngine;
using UnityEngine.UI;
using Table.DataModel;
using Table.Blo;

namespace AutoChess
{
    public class ChessPokedexItem : MonoBehaviour
    {
        public ChessPokedexPage ParentPage;
        public GameObject LineObj;
        public Text NameTxt;
        public TextureKit IconTex;
        public GroupSpriteKit[] GroupSprites;
        public GroupSpriteKit FrameSprite;
        public int HeroId { get { return _tableData.heroId; } }

        private HashSet<int> _propertyHash = new HashSet<int>();
        private TableAutoChessHero _tableData;
        private int _rare = 0;

        public void SetData(TableAutoChessHero table)
        {
            _tableData = table;
            var groupStr = table.acHeroGroups.Split(';');
            foreach (var item in groupStr)
            {
                var group = int.Parse(item);
                _propertyHash.Add(group);
            }
            _rare = table.rare;
            NameTxt.text = TableHeroBlo.GetHeroName(table.heroId);
            IconTex.TextureName = Table.Blo.TableHeroBlo.GetHeroIcon(table.heroId, 0);
            var groups = table.acHeroGroups.Split(';');
            int i = 0;
            for (; i < groups.Length; i++)
            {
                var group = int.Parse(groups[i]);
                var tableGroup = AutoChessBlo.GetGroup(group);
                if(tableGroup != null)
                {
                    GroupSprites[i].gameObject.SetActive(true);
                    GroupSprites[i].SetSpriteName(tableGroup.altasName);
                }
            }
            for (; i < GroupSprites.Length; i++)
            {
                GroupSprites[i].gameObject.SetActive(false);
            }
            var tableRare = AutoChessBlo.GetTableRareByRare(table.rare);
            FrameSprite.SetSpriteName(tableRare.rareFrameIcon);
        }

        public bool IsChoosedItem(int rare, int group, int type)
        {
            if (rare != 0 && rare != _rare)
                return false;
            if (group != -1 && !_propertyHash.Contains(group))
                return false;
            if (type != -1 && !_propertyHash.Contains(type))
                return false;

            return true;
        }

        public void OnSelect()
        {
            ParentPage.OnSelectHero(_tableData);
        }
    }
}
